package com.merelyb.task;

import com.merelyb.task.handle.affpay.AffpayGetHandler;
import com.merelyb.task.handle.dictarea.DictAreaGetHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.commons.cli.*;

/**
 * task启动类，所有的调度任务请从本类启动
 * Created by donghang on 15/7/9.
 * simple command: -v -t 1234 dh 2012-07-10
 */
public class RunTask {

    private static Logger logger = LoggerFactory.getLogger("task");

    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext(
                        new String[]{"classpath*:spring/spring*.xml"});


        try {
            Options options = new Options();
            options.addOption("v", "version", false, "show version");
            options.addOption("h", "help", false, "show help");
            options.addOption("getDictArea", false, "获取行政区域明细");
            options.addOption("affpay", false, "获取Affpay信息");

            CommandLineParser parser = new DefaultParser();
            CommandLine cmd = parser.parse(options, args);

//            AffpayGetHandler affpayGetHandler1 = new AffpayGetHandler();
//            affpayGetHandler1.syncAffpay();

            if (cmd.hasOption("version") || cmd.hasOption("v")) {

                logger.warn("warn2");
                logger.info("Entering application2.");
                logger.debug(">>>>>debug2");
                logger.error("error");
                logger.error("error2");
                System.out.println("common-task v1.0");
                System.exit(0);

            } else if (cmd.hasOption("help") || cmd.hasOption("h")) {
                new HelpFormatter().printHelp("data-task", options, true);
                System.exit(0);
            } else if (cmd.hasOption("getDictArea")) {
                DictAreaGetHandler dictAreaGetHandler = (DictAreaGetHandler) context.getBean("dictAreaGetHandler");
                dictAreaGetHandler.getDictArea();
                System.exit(0);
            } else if (cmd.hasOption("affpay")) {
                AffpayGetHandler affpayGetHandler = (AffpayGetHandler) context.getBean("affpayGetHandler");
                affpayGetHandler.syncAffpay();
                System.exit(0);
            }
            System.out.println(" Try 'java -jar task.jar -h' for more information");
        } catch (Exception e) {
            logger.error("task启动函数内部出错，异常为:", e);

        }
    }

}